﻿using Alipay.AopSdk.F2FPay.Business;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.IdentityModel.Tokens;
using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.Base;
using SmartWMS.Domain.BasicsDatum;
using SmartWMS.Domain.SystemSet;
using SmartWMS.Domain.WarehouseManage;
using SmartWMS.EntityFramework;
using SmartWMS.Server.BasicsDatumServer.CategoryService.Dto;
using SmartWMS.Server.BasicsDatumServer.LocationServer.Dto;
using SmartWMS.Server.Repository;
using SmartWMS.Server.SystemSets.Dictionary.Dto;
using SmartWMS.Server.SystemSets.StaffServices.Dto;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace SmartWMS.Server.BasicsDatumServer.ProductService
{
    public class CategoryService : ICategoryService
    {
        private readonly IRepository<Category> _categoryRepository;
        private readonly SmartWMSContext _smartHospitalContext;


        public CategoryService(SmartWMSContext smartHospitalContext, IRepository<Category> categoryRepository)
        {
            _smartHospitalContext = smartHospitalContext;
            _categoryRepository = categoryRepository;
        }

        //// <summary>
        /// 显示查询类别列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<List<CategoryInput>>> CategoryListAsync(Select input)
        {
            var list = await _categoryRepository.GetAllListAsync();
            if (!string.IsNullOrEmpty(input.code))
            {
                list = list.Where(m => m.CategoryCode.Contains(input.code)).ToList();
            }
            if (!string.IsNullOrEmpty(input.name))
            {
                list = list.Where(m => m.CategoryName.Contains(input.name)).ToList();
            }
            int totalCount = list.Count();
            int pageCount = (int)Math.Ceiling(totalCount / (double)input.pageSize);
            list = list.Skip((input.pageIndex - 1) * input.pageSize).Take(input.pageSize).ToList();
            var response = list.MapToList<CategoryInput>();
            return response != null ? Response<List<CategoryInput>>.Ok(response, totalCount) : Response<List<CategoryInput>>.Fail("查询失败");
        }

        /// <summary>
        /// 绑定下拉
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<List<CategoryInput>>> CategoryList()
        {
            var list = await _categoryRepository.GetAllListAsync();
            var response = list.MapToList<CategoryInput>();
            return response != null ? Response<List<CategoryInput>>.Ok(response) : Response<List<CategoryInput>>.Fail("查询失败");
        }

        /// <summary>
        /// 产品类别添加
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> AddCategory(CategoryInput input)
        {
            var category = input.MapTo<Category>();

            var addlist = await _categoryRepository.InsertAsync(category);

            return addlist > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
        }

        /// <summary>
        /// 产品类别删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> DeleteCategory(CategoryInput input)
        {
            var Category = input.MapTo<Category>();
            var result = await _categoryRepository.DeleteByIdAsync(Category);
            return result > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(result > 0);

        }

        /// <summary>
        /// 产品类别批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<Response<bool>> DeleteCategoryList(CategoryInput dto)
        {
            var sid = dto.DelId.Split(',');

            var list = new List<Category>();

            foreach (var item in sid)
            {
                var plist = _categoryRepository.GetListQuery().Where(t => t.Id == int.Parse(item)).ToList();
                list.AddRange(plist);
            }
            var result = _categoryRepository.DeleteAsync(list);
            return result.Result > 0 ? Task.FromResult(Response<bool>.Ok(result.Result > 0)) : Task.FromResult(Response<bool>.Fail(result.Result > 0));
        }
        
        /// <summary>
        /// 产品类别反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<Category>> GetCategoryById(KeyEntity id)
        {
            var category = await _categoryRepository.GetEntityByIdAsync(id);
            var result = category.MapTo<Category>();
            return Response<Category>.Ok(result);
        }

        /// <summary>
        /// 产品类别修改
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> UpdateCategory(CategoryInput dto)
        {
            var category = dto.MapTo<Category>();
            var result = await _categoryRepository.UpdateAsync(category);
            return result > 0 ? Response<bool>.Ok(result > 0) :
                  Response<bool>.Fail(result > 0);
        }
    }
}
